//
//  UILabel+Tools.m
//  BiaoPny
//
//  Created by QF_iOS on 2017/5/15.
//  Copyright © 2017年 BiaoPu. All rights reserved.
//

#import "UILabel+Tools.h"

@implementation UILabel (Tools)

/*>>>>>>>>>>>>>>>>>>>>>>>>>  创建label  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
//文字
+ (UILabel *)createLabelFrame:(CGRect)frame title:(NSString *)title titleColor:(UIColor*)tittleColor titleFont:(CGFloat)font bgColor:(UIColor *)bgColor radius:(CGFloat)radius inSuperView:(UIView *)superview isSizeToFit:(BOOL)isSizeToFit{
    
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.font = [UIFont systemFontOfSize:font];
    label.backgroundColor = bgColor;
    label.text = title;
    label.textColor = tittleColor;
    if (radius > 0) {
        label.layer.cornerRadius = radius;
        label.layer.masksToBounds = YES;
    }
    //判断是否需要自适应
    if (isSizeToFit) {
        //进行自适应
        //可设置的最大宽度
        CGSize size = CGSizeMake(kScreenWidth, MAXFLOAT);
        
        //获取文本属性
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:font],NSFontAttributeName, nil];
        //获取文本需要的size限制宽度
        CGSize autoSizeToFit = [title boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
        //更新label
        label.frame = CGRectMake(frame.origin.x, frame.origin.y, autoSizeToFit.width, autoSizeToFit.height);
        
    }
    
    [superview addSubview:label];
    
    return label;
    
    
}

+ (void)isSizeToFit:(UILabel *)label andWidth:(CGFloat)width
{
    //进行自适应
    label.numberOfLines = 0;
    //可设置的最大宽度
    CGSize size = CGSizeMake(width, MAXFLOAT);
    //获取文本属性
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:label.font,NSFontAttributeName, nil];
    //获取文本需要的size限制宽度
    CGSize autoSizeToFit = [label.text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
    //更新label
    label.frame = CGRectMake(label.frame.origin.x, label.frame.origin.y, autoSizeToFit.width, autoSizeToFit.height);
}

@end
